///<summary>
/// Developer Name : Mr.Teeradech Janwimalueng (http://teedech.blogspot.com, Thai Language Blog)
/// Date Created : 23 September 2007
/// Last Updated : -
/// License : Microsoft Permissive License (Ms-PL) v1.1
/// Version 1.0
///</summary>
///<remark>
///</remark>

using System;
using System.Collections.Generic;
using System.Text;

namespace CeasarShiftCipherCSharp
{
    class CeasarShiftCipherEngine
    {
        public string Encrypt(string plainText, int key)
        {
            //convert plaintext(string) to array of char
            char[] plainChar = plainText.ToCharArray();
            int[] ascii = new int[plainChar.Length];

            for (int count=0;count < plainChar.Length; count++)
            {
                //convert char to ASCII number
                ascii[count] = Convert.ToInt32(plainChar[count]);

                // Filter A-Z and a-z only
                // A-Z in ASCII is 65-90
                // a-z in ASCII is 97-122
                if (ascii[count] >= 65 && ascii[count] <= 90)
                {
                    ascii[count] = ((ascii[count] - 65 + key) % 26) + 65;

                    //code below for explain
                    //ascii(Count) -= 65 'assign A-Z as 0 to 25 in order
                    //ascii(Count) = (ascii[count] + key) % 26
                    //ascii(Count) += 65
                }
                else if (ascii[count] >= 97 && ascii[count] <= 122)
                {
                    ascii[count] = ((ascii[count] - 97 + key) % 26) + 97;

                    //code below for explain
                    //ascii(Count) -= 97 'assign A-Z as 0 to 25 in order
                    //ascii(Count) = (ascii[count] + key) % 26
                    //ascii(Count) += 97
                }

                // convert ASCII to char
                plainChar[count] = ((char)(ascii[count]));
            }

            // return array of char as string
            return new string(plainChar);
        }

        public string Decrypt(string cipherText, int key)
        {
            //convert ciphertext(String) to array of Char
            char[] cipherChar = cipherText.ToCharArray();
            int[] ascii = new int[cipherChar.Length + 1];

            for (int count = 0; count <= cipherChar.Length - 1; count++)
            {
                //convert char to ASCII number
                ascii[count] = Convert.ToInt32(cipherChar[count]);

                //Filter A-Z and a-z only
                //A-Z in ASCII is 65-90
                //a-z in ASCII is 97-122
                if (ascii[count] >= 65 & ascii[count] <= 90)
                {
                    ascii[count] = ((ascii[count] - 65 - (key % 26) + 26)) % 26 + 65;
                

                    //code below for explain
                    //Ascii(Count) -= 65 'assign A-Z as 0 to 25 in order
                    //key = key % 26
                    //Ascii(Count) = (Ascii(Count) - Key + 26) Mod 26
                    //Ascii(Count) += 65
                }
                else if (ascii[count] >= 97 & ascii[count] <= 122)
                {
                    ascii[count] = ((ascii[count] - 97 - (key % 26) + 26)) % 26 + 97;

                    //code below for explain
                    //ascii(count) -= 97 'assign a-z as 0 to 25 in order
                    //key = key % 26
                    //ascii(count) = (cscii(count) - Key + 26) % 26
                    //ascii(count) += 97
                }

                //convert ASCII to char
                cipherChar[count] = Convert.ToChar(ascii[count]);
            }

            //return array of char as string
            return new string(cipherChar);
        }
    }
}
